import { Controller, Post, Get, Body, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { MiniProgramService } from './miniprogram.service';
import { WxLoginRequestDto, WxLoginResponseDto, PaginationDto } from './dto';

@ApiTags('小程序管理')
@Controller()
export class MiniProgramController {
  constructor(private readonly miniprogramService: MiniProgramService) { }

  @Post('login')
  @ApiOperation({ summary: '微信小程序登录' })
  @ApiResponse({ status: 200, description: '登录成功', type: WxLoginResponseDto })
  async login(@Body() loginDto: WxLoginRequestDto): Promise<WxLoginResponseDto> {
    return this.miniprogramService.login(loginDto);
  }

  @Get('config')
  @ApiOperation({ summary: '获取小程序配置' })
  @ApiResponse({ status: 200, description: '获取配置成功' })
  getConfig() {
    return this.miniprogramService.getConfig();
  }

  @Get('user/info')
  @ApiOperation({ summary: '获取用户信息' })
  @ApiResponse({ status: 200, description: '获取用户信息成功' })
  getUserInfo(@Query('openid') openid: string) {
    return this.miniprogramService.getUserInfo(openid);
  }

  @Post('user/update')
  @ApiOperation({ summary: '更新用户信息' })
  @ApiResponse({ status: 200, description: '更新用户信息成功' })
  updateUserInfo(@Body() userInfo: any) {
    return this.miniprogramService.updateUserInfo(userInfo);
  }

  @Post('phone')
  @ApiOperation({ summary: '获取手机号' })
  @ApiResponse({ status: 200, description: '获取手机号成功' })
  getPhoneNumber(@Body() data: { code: string; encryptedData: string; iv: string }) {
    return this.miniprogramService.getPhoneNumber(data);
  }

  @Get('sessions')
  @ApiOperation({ summary: '获取会话列表' })
  @ApiResponse({ status: 200, description: '获取会话列表成功' })
  getSessions(@Query() paginationDto: PaginationDto) {
    return this.miniprogramService.getSessions(paginationDto);
  }

  @Get('sessions/:id')
  @ApiOperation({ summary: '获取会话详情' })
  @ApiResponse({ status: 200, description: '获取会话详情成功' })
  getSession(@Param('id') id: string) {
    return this.miniprogramService.getSession(+id);
  }
}
